<?php
class tuser extends CI_Model{
	var $user = "t_user";

	function __construct()
	{
		parent::__construct();
	}
	
	function setData($id_user,$nama_user,$password_user,$email_user,$telp_user,$alamat_user,$id_sektor,$akses_user,$tgl_buat,$enable)
	{
		$this->id_user= $id_user;
		$this->nama_user= $nama_user;
		$this->password_user= $password_user;
		$this->email_user= $email_user;
		$this->telp_user= $telp_user;
		$this->alamat_user= $alamat_user;
		$this->id_sektor= $id_sektor;
		$this->akses_user= $akses_user;
		$this->tgl_buat= $tgl_buat;
		$this->enable= $enable;
	}
	
	function getList($page){
		$strSql = "
			SELECT t.*,skt.name as nama_sektor
			FROM t_user t
			inner join(
				select kepolisian.id,kepolisian.name
				from (
				select rs.id_resort as id, rs.nama_resort as name
				from m_resort rs
				union all
				select sk.id_sektor as id, sk.nama_sektor as name
				from m_sektor sk
				where sk.id_resort <> 0
			  ) kepolisian
			) skt on t.id_sektor = skt.id
			limit ".$page."
		";
		$query = $this->db->query($strSql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
	
	function create()
	{		
		$arrayData = array(
			'id_user'=>$this->id_user,
			'nama_user'=>$this->nama_user,
			'password_user'=>$this->password_user,
			'email_user'=>$this->email_user,
			'telp_user'=>$this->telp_user,
			'alamat_user'=>$this->alamat_user,
			'id_sektor'=>$this->id_sektor,
			'akses_user'=>$this->akses_user,
			'tgl_buat'=>$this->tgl_buat,
			'enable'=>$this->enable
		);
		return $this->db->insert($this->user, $arrayData);
	}
	
	function update($id_user)
	{
		$arrayData = array(
			'nama_user'=>$this->nama_user,
			'password_user'=>$this->password_user,
			'email_user'=>$this->email_user,
			'telp_user'=>$this->telp_user,
			'alamat_user'=>$this->alamat_user,
			'id_sektor'=>$this->id_sektor,
			'akses_user'=>$this->akses_user,
			'tgl_buat'=>$this->tgl_buat,
			'enable'=>$this->enable
		);
		$this->db->where('id_user', $id_user);
		return $this->db->update($this->user, $arrayData);
	}
        
	function remove($id_user)
	{
		$this->db->where('id_user', $id_user);
		return $this->db->delete($this->user);
	}	
	
	function detail($id_user)
	{
		$this->db->where('id_user', $id_user);
		$query = $this->db->get($this->user);	
		return $query->result_array();
	}
	
	function checkuser($username,$password){
		$this->db->where('nama_user', $username);
		$this->db->where('password_user', $password);
		$this->db->where('enable', "1");
		$query = $this->db->get($this->user);
		
		if($query->num_rows == 1){
			$row = $query->row_array();
			$data = array(
				'username' => $row['nama_user'],
				'sektor' => $row['id_sektor'],
				'token' => md5($row['password_user'])
			);
			$this->session->set_userdata($data);
			
		    return true;
		}
	}
	
	function getListSearch($name){
		$strSql = "
			SELECT t.*,skt.name as nama_sektor
			FROM t_user t
			inner join(
				select kepolisian.id,kepolisian.name
				from (
				select rs.id_resort as id, rs.nama_resort as name
				from m_resort rs
				union all
				select sk.id_sektor as id, sk.nama_sektor as name
				from m_sektor sk
			  ) kepolisian
			) skt on t.id_sektor = skt.id
			where t.nama_user like '%".$name."%'
		";
		$query = $this->db->query($strSql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
}
?>